<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无佈(285753421@qq.com)
 * Date: 15-2-7
 * Time: 上午9:28
 */

class user extends MY_Api_Controller{

    function __construct(){
        parent::__construct();
        $this->load->library('tank_auth');
    }



    function do_login($token){
        $this->_verify($token);
        $this->load->helper('validate');
        $account = $this->input->post('account');
        $password = $this->input->post('password');
        $check_mobile = is_mobile($account);
        $logined=false;
        if (!$check_mobile['status']) {
            $check_account = is_email($account);
            if (!$check_account['status']) {
                $this->_json(array('state'=>false,'msg'=>'account error'));
            }else{
                $logined=$this->tank_auth->login($account,$password,true,false,true,false);
            }
        }else{
            $logined=$this->tank_auth->login($account,$password,true,false,false,true);
        }
        if ($logined)
        {
            $user_id = $this->tank_auth->get_user_id();
            $this->user_action($user_id, 'user_login');
            $this->load->model('api/apps');
            $operate_token = md5(sha1($token));
            $this->apps->set_operate_token($token,$operate_token,$user_id);
            $this->_json(array('state'=>true,'user_id'=>$user_id,'operate_token'=>$operate_token));
        } else {
            $this->_json(array('state'=>false));
        }

    }

    function do_register($token,$is_mobile = 0){
        $this->_verify($token);
        $this->load->helper('validate');
        if($is_mobile == 0){
            $username = $this->input->post('email');
            $username_type = 'email';
        }else{
            $username = $this->input->post('mobile');
            $username_type = 'mobile';
        }

        if(!$username)
            $this->_json(array('state'=>false,'msg'=>'账户名错误'));
        $user_type = $this->input->post('user_type');
        $password = trim($this->input->post('password'));
        $re_password = trim($this->input->post('re_password'));
        $back = null;

        if ($password != $re_password) {
            $this->_json(array('state'=>false,'msg'=>'密码和确认密码不一致'));
        }
        $check_password = is_password($password);
        if (!$check_password['status']) {
            $this->_json(array('state'=>false,'msg'=>'密码格式错误'));
        }

        $data = array(
            'username'  =>  $username,
            'user_type' =>  $user_type,
            'password'  =>  $password
        );
        if($username_type=='email'){
            $back = $this->_user_email($data);
        }else{
            $back = $this->_user_mobile($data);
        }
        if (!is_null($back)) {
            $this->_json(array('state'=>true));
        }else{
            $this->_json(array('state'=>false));
        }
    }

    function get_user($user_id,$token){

    }

    function do_update_user($user_id,$operate_token){
        $this->_verify_operate_token($user_id,$operate_token);
        $data = array(
            'id' => $user_id,
            'address' => trim($this->input->post('address')),
            'nickname'	=> trim($this->input->post('nickname')),
            'qq'	=> trim($this->input->post('qq')),
            'introduce'	=> trim($this->input->post('introduce')),
            'province'  =>  trim($this->input->post('province')),
            'city'  =>  trim($this->input->post('introduce')),
            'province_now'  => trim($this->input->post('province_now')),
            'city_now'  => trim($this->input->post('city_now')),
        );

        if ($this->input->post('sex') == 1 || $this->input->post('sex') == 0) {
            $data['sex'] = $this->input->post('sex');
        }

        if ($this->user_model->save($data)) {
            $this->_json(array('state'=>true));
        }
        $this->_json(array('state'=>false));
    }

    function get_file($user_id,$operate_token,$classify= 0,$page=1,$page_size =10){
        $this->_verify_operate_token($user_id,$operate_token);
        $this->load->model('user/user_file');
        $offset = ($page-1)*$page_size;
        $condition = array(
            'user_id'   => $user_id,
        );
        if($classify){
            $condition['file_classify'] = $classify;
        }
        $my_file = $this->user_file->get_file($condition,$page_size,$offset);
        for($i=0;$i<count($my_file);$i++){
            $my_file[$i]['collect'] = $this->user_file->get_like_by_id($this->user_id,$my_file[$i]['id'],1);
            $my_file[$i]['like'] = $this->user_file->get_like_by_id($this->user_id,$my_file[$i]['id'],2);
        }
        $this->_json(array('state'=>true,'my_file'=>$my_file));
    }

    function get_order($user_id,$operate_token,$state='all',$page=1,$page_size =5){
        $this->_verify_operate_token($user_id,$operate_token);
        $this->load->model('user/user_order');
        $offset = ($page-1)*$page_size;
        if($state == 'all'){
            $my_order = $this->user_order->select($user_id,0,0,$page_size,$offset);
        }else{
            $my_order = $this->user_order->select($user_id,$state,0,$page_size,$offset);
        }
        $this->load->helper('order');
        $this->load->model('public/file_base');
        $i = 0;
        foreach($my_order as $order){
            $message[$i] = json_decode($order['info'],true);
            $my_order[$i]['message'] = $message[$i];
            $i++;
        }
        $this->_json(array('state'=>true,'my_order'=>$my_order));
    }

    function place_order(){

    }

    function do_update_order(){

    }

}